import {Component, OnInit, ViewChild} from '@angular/core';
import {LifepaymentModalViewComponent} from "../lifepayment-modal-view/lifepayment-modal-view.component";
import {MeterService} from "../../service/meter.service";
import {USER} from "../../service/user.service";
import {LocalStorage} from "../../app.local-storage";
import {PageParam} from "../../model/params.model";
declare var waveLoading:any;

@Component({
  selector: 'app-my-lifepayment-view',
  templateUrl: './my-lifepayment-view.component.html',
  styleUrls: ['./my-lifepayment-view.component.scss']
})
export class MyLifepaymentViewComponent implements OnInit {
  @ViewChild(LifepaymentModalViewComponent)public lifePaymentModal:any;
  currentPage:number = 1;
  inited:boolean=false;
  pageParam:PageParam;
  pagination={
    page:1,
    totalPage:1
  }
  bills:any;

  contractId:string;
  showPaymentModal(){
    this.lifePaymentModal.showModal();
  }

  constructor(private METER:MeterService,private USER:USER,private ls:LocalStorage) {
    this.inited = false;
  }
  onPageChanged(v){
    console.log('onPageChanged',v.data.page)
    this.pagination.page = v.data.page
    this.pageParam.page = this.pagination.page-1
    this.queryList()
  }
  queryList(){
    this.METER.getList(resp=>{
      this.pagination.totalPage = resp.totalPages;
      this.bills =[];
      for (let i=0;i<resp.content.length;i++){
        if (i%2==0){
          let item = {
            month:resp.content[i].month,
            bill:[]
          }
          item.bill.push(resp.content[i])
          item.bill.push(resp.content[i+1])
          this.bills.push(item)
        }
      }
      console.log(this.bills)
    },this.contractId,this.pageParam)
  }

  ngOnInit() {
    let that = this;
      waveLoading.load1.init({
        target: '#ebalance',
        color: '#F5B88D',
        showText: true
      });
      waveLoading.load1.draw();
      waveLoading.load2.init({
        target: '#wbalance',
        color: '#C3DFFE',
        showText: true
      });
      waveLoading.load2.draw();
      this.inited = true;
    this.USER.refreshContract.subscribe(v=> {
      console.log("USER.refreshContract",v);
      this.contractId = v.id;
      this.pageParam = new PageParam()
      this.pageParam.size = 6;
      this.METER.getBalance(this.contractId,resp=>{
        waveLoading.load1.setProgress(resp.powerDegree)
        waveLoading.load1.setProgress(resp.waterDegree)
      })
      this.queryList()
    })
  }

}
